﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bzzb.Model;
using Bzzb.Util;
using FreeSql.Internal.Model;
using Masuit.Tools.DateTimeExt;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Bzzb.Controllers
{
    public class DingQiWeiHuZhuangTaiController : BaseController
    {
        private readonly ILogger<DingQiWeiHuZhuangTaiController> _logger;
        public DingQiWeiHuZhuangTaiController(ILogger<DingQiWeiHuZhuangTaiController> logger, IFreeSql fsql) : base(fsql)
        {
            this._logger = logger;
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Index([FromForm] long bzzbtid, [FromForm] string content, [FromForm] int pageNumber = 1, [FromForm] int pageSize = 10)
        {
            DynamicFilterInfo dyfilter = JsonConvert.DeserializeObject<DynamicFilterInfo>(@"
{
  'Logic' : 'Or',
  'Filters' :
  [
    {
      'Field' : 'Kzxmc',
      'Operator' : 'Contains',
      'Value' : '" + content + @"',
    }
  ]
}
");
            // 1.保障车辆 2.保障设备 3.保障构件车辆 4.保障构件设备
            string[] bzzbt_array = { "保障车辆", "保障设备", "保障构件车辆", "保障构件设备" };
            List<DingQiWeiHuZhuangTai> dingqiweihuzhuangtais = new();
            List<DingQiKongZhiXiang> dingqikongzhixiangs = await _fsql.Select<DingQiKongZhiXiang>().Where(dqkzx => dqkzx.Bzzbtid == bzzbtid).WhereDynamicFilter(dyfilter).Count(out var total).Page(pageNumber, pageSize).ToListAsync();
            if (dingqikongzhixiangs != null)
            {
                foreach (var dingqikongzhixiang in dingqikongzhixiangs)
                {
                    DingQiWeiHuZhuangTai dingqiweihuzhuangtai = new();
                    dingqiweihuzhuangtai.Id = dingqikongzhixiang.Id;
                    dingqiweihuzhuangtai.Bzzbtid = dingqikongzhixiang.Bzzbtid;
                    dingqiweihuzhuangtai.Bzzbid = dingqikongzhixiang.Bzzbid;
                    dingqiweihuzhuangtai.Jtid = dingqikongzhixiang.Jtid;
                    dingqiweihuzhuangtai.Gjid = dingqikongzhixiang.Gjid;
                    dingqiweihuzhuangtai.Kzjwybs = dingqikongzhixiang.Kzjwxbs;
                    dingqiweihuzhuangtai.Kzxmc = dingqikongzhixiang.Kzxmc;
                    dingqiweihuzhuangtai.Kzjlx = bzzbt_array[bzzbtid - 1];
                    dingqiweihuzhuangtai.Kzlx = dingqikongzhixiang.Kzlx;
                    dingqiweihuzhuangtai.Kzbz = dingqikongzhixiang.Kzbz;
                    dingqiweihuzhuangtai.Qssj = dingqikongzhixiang.Qssj;
                    dingqiweihuzhuangtai.Scgzrq = dingqikongzhixiang.Scgzrq;
                    dingqiweihuzhuangtai.Kzsx = dingqikongzhixiang.Kzsx;
                    dingqiweihuzhuangtai.Kzxx = dingqikongzhixiang.Kzxx;
                    if (bzzbtid == 1)
                    {
                        if (dingqiweihuzhuangtai.Bzzbid == 1)
                        {
                            // 电源车
                            DianYuanChe dianyuanche = await _fsql.Select<DianYuanChe>().Where(dyc => dyc.Id == dingqikongzhixiang.Jtid).ToOneAsync();
                            BaoZhangZhuangBeiIOT baozhangzhuangbeiiot = await _fsql.Select<BaoZhangZhuangBeiIOT>().Where(bzzbiot => bzzbiot.Bzzbtid == bzzbtid && bzzbiot.Bzzbid == 1 && bzzbiot.Jtid == dianyuanche.Id && bzzbiot.Gjid == 0).OrderBy("id desc").ToOneAsync();
                            if (baozhangzhuangbeiiot != null) {
                                dingqiweihuzhuangtai.Clmc = dianyuanche.Clmc;
                                dingqiweihuzhuangtai.Cph = dianyuanche.Cph;
                                dingqiweihuzhuangtai.Iotid = baozhangzhuangbeiiot.Id;
                                dingqiweihuzhuangtai.Syrq = baozhangzhuangbeiiot.Created;
                                dingqiweihuzhuangtai.Sydd = baozhangzhuangbeiiot.Zxwz;
                                dingqiweihuzhuangtai.Sysj = dianyuanche.Sysj;
                                JiShiXiang jishixiang = await _fsql.Select<JiShiXiang>().Where(jsx => jsx.Id == dingqikongzhixiang.Jsxid).ToOneAsync();
                                dingqiweihuzhuangtai.Jsxmc = jishixiang.Name;
                                dingqiweihuzhuangtai.Jldw = jishixiang?.Jldw;
                                dingqiweihuzhuangtai.Ljsysj = baozhangzhuangbeiiot.Ljsysj;
                                dingqiweihuzhuangtai.Lhdw = baozhangzhuangbeiiot.Lhdw; // 累计使用量
                                dingqiweihuzhuangtai.Ldw = baozhangzhuangbeiiot.Ldw; // 累计使用量
                                dingqiweihuzhuangtai.Kmdw = baozhangzhuangbeiiot.Kmdw; // 累计使用量
                                dingqiweihuzhuangtai.Cidw = baozhangzhuangbeiiot.Cidw; // 累计使用量
                                dingqiweihuzhuangtai.Tiandw = baozhangzhuangbeiiot.Tiandw; // 累计使用量
                                dingqiweihuzhuangtai.Hdw = baozhangzhuangbeiiot.Hdw; // 累计使用量
                                dingqiweihuzhuangtai.Totalsj = baozhangzhuangbeiiot.Totalsj; // 累计使用量
                                dingqiweihuzhuangtai.Totalsyl = baozhangzhuangbeiiot.Totalsyl; // 累计使用量
                            }
                        }
                        else
                        {
                            // 加油车
                            JiaYouChe jiayouche = await _fsql.Select<JiaYouChe>().Where(jyc => jyc.Id == dingqikongzhixiang.Jtid).ToOneAsync();
                            dingqiweihuzhuangtai.Clmc = jiayouche.Clmc;
                            dingqiweihuzhuangtai.Cph = jiayouche.Cph;
                            BaoZhangZhuangBeiIOT baozhangzhuangbeiiot = await _fsql.Select<BaoZhangZhuangBeiIOT>().Where(bzzbiot => bzzbiot.Bzzbtid == bzzbtid && bzzbiot.Bzzbid == 2 && bzzbiot.Jtid == jiayouche.Id && bzzbiot.Gjid == 0).OrderBy("id desc").ToOneAsync();
                            if (baozhangzhuangbeiiot != null) {
                                dingqiweihuzhuangtai.Iotid = baozhangzhuangbeiiot.Id;
                                dingqiweihuzhuangtai.Syrq = baozhangzhuangbeiiot.Created;
                                dingqiweihuzhuangtai.Sydd = baozhangzhuangbeiiot.Zxwz;
                                dingqiweihuzhuangtai.Sysj = jiayouche.Sysj;
                                JiShiXiang jishixiang = await _fsql.Select<JiShiXiang>().Where(jsx => jsx.Id == dingqikongzhixiang.Jsxid).ToOneAsync();
                                dingqiweihuzhuangtai.Jsxmc = jishixiang.Name;
                                dingqiweihuzhuangtai.Jldw = jishixiang == null ? null : jishixiang.Jldw;
                                dingqiweihuzhuangtai.Ljsysj = baozhangzhuangbeiiot.Ljsysj;
                                dingqiweihuzhuangtai.Lhdw = baozhangzhuangbeiiot.Lhdw; // 累计使用量
                                dingqiweihuzhuangtai.Ldw = baozhangzhuangbeiiot.Ldw; // 累计使用量
                                dingqiweihuzhuangtai.Kmdw = baozhangzhuangbeiiot.Kmdw; // 累计使用量
                                dingqiweihuzhuangtai.Cidw = baozhangzhuangbeiiot.Cidw; // 累计使用量
                                dingqiweihuzhuangtai.Tiandw = baozhangzhuangbeiiot.Tiandw; // 累计使用量
                                dingqiweihuzhuangtai.Hdw = baozhangzhuangbeiiot.Hdw; // 累计使用量
                                dingqiweihuzhuangtai.Totalsj = baozhangzhuangbeiiot.Totalsj; // 累计使用量
                                dingqiweihuzhuangtai.Totalsyl = baozhangzhuangbeiiot.Totalsyl; // 累计使用量
                            }
                        }
                    }
                    else if (bzzbtid == 2)
                    {
                        BaoZhangSheBei baozhangshebei = await _fsql.Select<BaoZhangSheBei>().Where(bzsb => bzsb.Id == dingqikongzhixiang.Jtid).ToOneAsync();
                        BaoZhangZhuangBeiIOT baozhangzhuangbeiiot = await _fsql.Select<BaoZhangZhuangBeiIOT>().Where(bzzbiot => bzzbiot.Bzzbtid == bzzbtid).OrderBy("id desc").ToOneAsync();
                        if (baozhangzhuangbeiiot != null) {
                            dingqiweihuzhuangtai.Iotid = baozhangzhuangbeiiot.Id;
                            dingqiweihuzhuangtai.Syrq = baozhangzhuangbeiiot.Created;
                            dingqiweihuzhuangtai.Sydd = baozhangzhuangbeiiot.Zxwz;
                            dingqiweihuzhuangtai.Sysj = baozhangshebei.Sysj;
                            JiShiXiang jishixiang = await _fsql.Select<JiShiXiang>().Where(jsx => jsx.Id == dingqikongzhixiang.Jsxid).ToOneAsync();
                            dingqiweihuzhuangtai.Jsxmc = jishixiang.Name;
                            dingqiweihuzhuangtai.Jldw = jishixiang == null ? null : jishixiang.Jldw;
                            dingqiweihuzhuangtai.Ljsysj = baozhangzhuangbeiiot.Ljsysj;
                            dingqiweihuzhuangtai.Lhdw = baozhangzhuangbeiiot.Lhdw; // 累计使用量
                            dingqiweihuzhuangtai.Ldw = baozhangzhuangbeiiot.Ldw; // 累计使用量
                            dingqiweihuzhuangtai.Kmdw = baozhangzhuangbeiiot.Kmdw; // 累计使用量
                            dingqiweihuzhuangtai.Cidw = baozhangzhuangbeiiot.Cidw; // 累计使用量
                            dingqiweihuzhuangtai.Tiandw = baozhangzhuangbeiiot.Tiandw; // 累计使用量
                            dingqiweihuzhuangtai.Hdw = baozhangzhuangbeiiot.Hdw; // 累计使用量
                            dingqiweihuzhuangtai.Totalsj = baozhangzhuangbeiiot.Totalsj; // 累计使用量
                            dingqiweihuzhuangtai.Totalsyl = baozhangzhuangbeiiot.Totalsyl; // 累计使用量
                        }
                    }
                    else if (bzzbtid == 3 || bzzbtid == 4)
                    {
                        GouJianZhuangBei goujianzhuangbei = await _fsql.Select<GouJianZhuangBei>().Where(gjzb => gjzb.Id == dingqikongzhixiang.Gjid).ToOneAsync();
                        BaoZhangZhuangBeiIOT baozhangzhuangbeiiot = await _fsql.Select<BaoZhangZhuangBeiIOT>().Where(bzzbiot => bzzbiot.Bzzbtid == bzzbtid).OrderBy("id desc").ToOneAsync();
                        if (baozhangzhuangbeiiot != null) {
                            dingqiweihuzhuangtai.Iotid = baozhangzhuangbeiiot.Id;
                            dingqiweihuzhuangtai.Syrq = dingqikongzhixiang.Qssj;
                            dingqiweihuzhuangtai.Sydd = "";
                            dingqiweihuzhuangtai.Sysj = "";
                            JiShiXiang jishixiang = await _fsql.Select<JiShiXiang>().Where(jsx => jsx.Id == dingqikongzhixiang.Jsxid).ToOneAsync();
                            dingqiweihuzhuangtai.Jsxmc = jishixiang.Name;
                            dingqiweihuzhuangtai.Jldw = jishixiang == null ? null : jishixiang.Jldw;
                            dingqiweihuzhuangtai.Ljsysj = baozhangzhuangbeiiot.Ljsysj;
                            dingqiweihuzhuangtai.Lhdw = baozhangzhuangbeiiot.Lhdw; // 累计使用量
                            dingqiweihuzhuangtai.Ldw = baozhangzhuangbeiiot.Ldw; // 累计使用量
                            dingqiweihuzhuangtai.Kmdw = baozhangzhuangbeiiot.Kmdw; // 累计使用量
                            dingqiweihuzhuangtai.Cidw = baozhangzhuangbeiiot.Cidw; // 累计使用量
                            dingqiweihuzhuangtai.Tiandw = baozhangzhuangbeiiot.Tiandw; // 累计使用量
                            dingqiweihuzhuangtai.Hdw = baozhangzhuangbeiiot.Hdw; // 累计使用量
                            dingqiweihuzhuangtai.Totalsj = baozhangzhuangbeiiot.Totalsj; // 累计使用量
                            dingqiweihuzhuangtai.Totalsyl = baozhangzhuangbeiiot.Totalsyl; // 累计使用量
                        }
                    }
                    else { }

                    dingqiweihuzhuangtais.Add(dingqiweihuzhuangtai);
                }
            }
            JObject data = new();
            data.Add("total", total);
            data.Add("list", dingqiweihuzhuangtais == null ? null : JArray.FromObject(dingqiweihuzhuangtais));
            return RenderJson(data: data);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Save([FromForm] long dqkzxid, [FromForm] DingQiWeiHuJiLu dingqiweihujilu)
        {
            int affrows = 0;
            _fsql.Transaction(() =>
            {
                // 插入定期维护记录
                _fsql.Insert(dingqiweihujilu).ExecuteAffrows();
                // 更新定期控制项起始时间
                _fsql.Update<DingQiKongZhiXiang>(dingqiweihujilu.Id).Set(dqkzx => new DingQiKongZhiXiang
                {
                    Gzsj = 0,
                    Scgzrq = dingqiweihujilu.Qssj,
                    Qssj = DateTime.Now
                }).ExecuteAffrows();
                _logger.LogInformation("维护状态更新定期控制项时间：" + _fsql.Update<DingQiKongZhiXiang>(dingqiweihujilu.Id).Set(dqkzx => new DingQiKongZhiXiang
                {
                    Scgzrq = dingqiweihujilu.Qssj,
                    Qssj = DateTime.Now
                }).ToSql());
            });

            return RenderJson(data: affrows);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Finished([FromForm] long dqkzxid, [FromForm] DingQiWeiHuJiLu dingqiweihujilu)
        {
            int affrows = 0;
            _fsql.Transaction(() =>
            {
                #region 先前代码
                /**
                // 插入定期维护记录
                _fsql.Insert(dingqiweihujilu).ExecuteAffrows();
                // 更新定期控制项起始时间
                _fsql.Update<DingQiKongZhiXiang>(dqkzxid).Set(dqkzx => new DingQiKongZhiXiang
                {
                    Scgzrq = dingqiweihujilu.Scgzrq,
                    Qssj = dingqiweihujilu.Qssj
                }).ExecuteAffrows();
                */
                #endregion

                // 根据对应单位更新具体id对应的数值 // 1.保障车辆 2.保障设备 3.保障构件车辆 4.保障构件设备  1)电源车 2）加油车
                _fsql.Update<BaoZhangZhuangBeiIOT>(dingqiweihujilu.Iotid).Set(bzzbiot => new BaoZhangZhuangBeiIOT
                {
                    Yh = 0,
                    Yxyl = 0 + "-" + 100,
                    Kmdw = dingqiweihujilu.Kmdw,
                    Lhdw = dingqiweihujilu.Lhdw,
                    Ldw = dingqiweihujilu.Ldw,
                    Tiandw = dingqiweihujilu.Tiandw,
                    Hdw = dingqiweihujilu.Hdw,
                    Cidw = dingqiweihujilu.Cidw
                }).ExecuteAffrows();
                if (dingqiweihujilu.Jldw.ToLower().Equals("l"))
                {
                    GenerateDataController.L = 100;
                }
                else if (dingqiweihujilu.Jldw.ToLower().Equals("km"))
                {
                    GenerateDataController.KM = 0; // 累计使用量
                }
                else if (dingqiweihujilu.Jldw.ToLower().Equals("次"))
                {
                    GenerateDataController.CI = 0; // 累计使用量
                }
                else { }
                //GenerateDataController.L = 100;
                //GenerateDataController.KM = 0;
                //GenerateDataController.CI = 0;
                //GenerateDataController.YXYL = 100;
        #region 以前代码
        /**
        if (dingqiweihujilu.Bzzbtid == 1)
        {
            if (dingqiweihujilu.Bzzbid == 1) { // 电源车
                if (dingqiweihujilu.Jldw.Equals("L/h"))
                {
                    _fsql.Update<DianYuanCheIOT>(dingqiweihujilu.Iotid).Set(dyciot => new DianYuanCheIOT { 
                        Yh = 0
                    }).ExecuteAffrows();
                    Console.WriteLine("电源车IOT Yh记录：" + _fsql.Update<DianYuanCheIOT>(dingqiweihujilu.Iotid).Set(dyciot => new DianYuanCheIOT
                    {
                        Yh = 0
                    }).ToSql());
                }
                else if (dingqiweihujilu.Jldw.Equals("L"))
                {
                    
                    _fsql.Update<DianYuanCheIOT>(dingqiweihujilu.Iotid).Set(dyciot => new DianYuanCheIOT
                    {
                        Yxyl = 0 + "-" + 100
                    }).ExecuteAffrows();
                }
                else if (dingqiweihujilu.Jldw.Equals("km"))
                {
                    // 每次+5
                    JiShuQi.Km = -5;
                }
                else if (dingqiweihujilu.Jldw.Equals("天"))
                {
                    // 从定期控制项起始时间，当前时间 - 定期控制项起始时间 = 分钟换算成天
                    _fsql.Update<DianYuanCheIOT>(dingqiweihujilu.Iotid).Set(dyciot => new DianYuanCheIOT
                    {
                        Created = DateTime.Now
                    }).ExecuteAffrows();
                }
                else if (dingqiweihujilu.Jldw.Equals("h"))
                {
                    // 从定期控制项起始时间，当前时间 - 定期控制项起始时间 = 分钟换算成小时
                    _fsql.Update<DianYuanCheIOT>(dingqiweihujilu.Iotid).Set(dyciot => new DianYuanCheIOT
                    {
                        Created = DateTime.Now
                    }).ExecuteAffrows();
                }
                else if (dingqiweihujilu.Jldw.Equals("次"))
                {
                    // 每次+1
                    JiShuQi.Ci = -1;
                }
                else
                {
                }
            }
            if (dingqiweihujilu.Bzzbid == 2)
            { // 加油车
                if (dingqiweihujilu.Jldw.Equals("L/h"))
                {
                    _fsql.Update<JiaYouCheIOT>(dingqiweihujilu.Iotid).Set(dyciot => new JiaYouCheIOT
                    {
                        Yh = 0
                    }).ExecuteAffrows();
                }
                else if (dingqiweihujilu.Jldw.Equals("L"))
                {

                    _fsql.Update<JiaYouCheIOT>(dingqiweihujilu.Iotid).Set(dyciot => new JiaYouCheIOT
                    {
                        Yxyl = 0 + "-" + 100
                    }).ExecuteAffrows();
                }
                else if (dingqiweihujilu.Jldw.Equals("km"))
                {
                    // 每次+5
                    JiShuQi.Km = -5;
                }
                else if (dingqiweihujilu.Jldw.Equals("天"))
                {
                    // 从定期控制项起始时间，当前时间 - 定期控制项起始时间 = 分钟换算成天
                    _fsql.Update<JiaYouCheIOT>(dingqiweihujilu.Iotid).Set(dyciot => new JiaYouCheIOT
                    {
                        Created = DateTime.Now
                    }).ExecuteAffrows();
                }
                else if (dingqiweihujilu.Jldw.Equals("h"))
                {
                    // 从定期控制项起始时间，当前时间 - 定期控制项起始时间 = 分钟换算成小时
                    _fsql.Update<JiaYouCheIOT>(dingqiweihujilu.Iotid).Set(dyciot => new JiaYouCheIOT
                    {
                        Created = DateTime.Now
                    }).ExecuteAffrows();
                }
                else if (dingqiweihujilu.Jldw.Equals("次"))
                {
                    // 每次+1
                    JiShuQi.Ci = -1;
                }
                else
                {
                }
            }
        }
        else if (dingqiweihujilu.Bzzbtid == 2)
        {
            if (dingqiweihujilu.Jldw.Equals("km"))
            {
                // 清零
                JiShuQi.Km = -5;
            }
            else if (dingqiweihujilu.Jldw.Equals("天"))
            {
                // 从定期控制项起始时间，当前时间 - 定期控制项起始时间 = 分钟换算成天
                _fsql.Update<DingQiKongZhiXiang>(dingqiweihujilu.Id).Set(dyciot => new DingQiKongZhiXiang
                {
                    Scgzrq = dingqiweihujilu.Qssj,
                    Qssj = DateTime.Now
                }).ExecuteAffrows();
            }
            else if (dingqiweihujilu.Jldw.Equals("h"))
            {
                // 从定期控制项起始时间，当前时间 - 定期控制项起始时间 = 分钟换算成小时
                _fsql.Update<DingQiKongZhiXiang>(dingqiweihujilu.Id).Set(dyciot => new DingQiKongZhiXiang
                {
                    Scgzrq = dingqiweihujilu.Qssj,
                    Qssj = DateTime.Now
                }).ExecuteAffrows();
            }
            else if (dingqiweihujilu.Jldw.Equals("次"))
            {
                // 每次+1
                JiShuQi.Ci = -1;
            }
            else
            {
            }
        }
        else if (dingqiweihujilu.Bzzbtid == 3 || dingqiweihujilu.Bzzbtid == 4)
        {
            _fsql.Update<DingQiKongZhiXiang>(dingqiweihujilu.Id).Set(dqkzx => new DingQiKongZhiXiang
            {
                Qssj = DateTime.Now,
                Scgzrq = dingqiweihujilu.Scgzrq
            }).ExecuteAffrows();
            //if (dingqiweihujilu.Jldw.Equals("km"))
            //{
            //    // 清零
            //    DingQiWeiHuZhuangTaiController.Km = 0;
            //}
            //else if (dingqiweihujilu.Jldw.Equals("天"))
            //{
            //    // 从定期控制项起始时间，当前时间 - 定期控制项起始时间 = 分钟换算成天
            //    _fsql.Update<BaoZhangSheBeiIOT>(dingqiweihujilu.Gjid).Set(dyciot => new BaoZhangSheBeiIOT
            //    {
            //        Created = DateTime.Now
            //    }).ExecuteAffrows();
            //}
            //else if (dingqiweihujilu.Jldw.Equals("h"))
            //{
            //    // 从定期控制项起始时间，当前时间 - 定期控制项起始时间 = 分钟换算成小时
            //    _fsql.Update<BaoZhangSheBeiIOT>(dingqiweihujilu.Gjid).Set(dyciot => new BaoZhangSheBeiIOT
            //    {
            //        Created = DateTime.Now
            //    }).ExecuteAffrows();
            //}
            //else if (dingqiweihujilu.Jldw.Equals("次"))
            //{
            //    // 每次+1
            //    DingQiWeiHuZhuangTaiController.Km = 0;
            //}
            //else
            //{
            //}
        }
        else { }*/
        #endregion
    });

            return RenderJson(data: affrows);
        }
    }
}
